package com.cal.utms.action.dealing;


import java.text.DateFormat;
import java.text.SimpleDateFormat;

import com.cal.utms.action.user_maintenance.LoginAction;
import com.cal.utms.dao.dealing.InstrumentDao;
import com.cal.utms.dao.maintenance.NewFundDao;
import com.cal.utms.dto.dealing.InsDateSetDto;
import com.cal.utms.entity.Fund;
import com.cal.utms.entity.Instrument;
import com.cal.utms.entity.InstrumentCouponDate;
import com.cal.utms.entity.InstrumentFloatingRate;
import com.cal.utms.enumType.InterestRate;
import com.cal.utms.model.dealing.InsDateSetModel;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class InsDateSetAction extends ActionSupport implements ModelDriven<InsDateSetDto> {

	private static final long serialVersionUID = 1L;

	private InsDateSetDto insDateSetDto = new InsDateSetDto();

	@Override
	public InsDateSetDto getModel() {
		return insDateSetDto;
	}
	
	public String getInsTypes(){
		int fundId = 0;
		if(insDateSetDto.getQueryOne()==null){
			insDateSetDto.setStatus("Fund ID is null");
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}else{
			try{
				fundId = Integer.parseInt(insDateSetDto.getQueryOne());
			}catch(NumberFormatException nfe){
				insDateSetDto.setStatus("Invalid Fund ID: "+insDateSetDto.getQueryOne());
				insDateSetDto.setSuccess(false);
				return Action.SUCCESS;
			}
		}		
		NewFundDao fundDao = new NewFundDao();
		insDateSetDto.setInsTypes(fundDao.getInsTypesForDealType(fundId, insDateSetDto.getQueryTwo()));
		insDateSetDto.setSuccess(true);
		return Action.SUCCESS;
	}
	
	public String getInInsCodes(){
		int fundId = 0;
		if(insDateSetDto.getQueryOne()==null){
			insDateSetDto.setStatus("Fund ID is null");
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}else{
			try{
				fundId = Integer.parseInt(insDateSetDto.getQueryOne());
			}catch(NumberFormatException nfe){
				insDateSetDto.setStatus("Invalid Fund ID: "+insDateSetDto.getQueryOne());
				insDateSetDto.setSuccess(false);
				return Action.SUCCESS;
			}
		}		
		InstrumentDao insDao = new InstrumentDao();
		insDateSetDto.setInsCodes(insDao.getInInsCodesForFund(fundId));
		insDateSetDto.setSuccess(true);
		return Action.SUCCESS;
	}
	
	public String getCouponDates(){
		System.out.println("Coupon Date Req from: "+LoginAction.getCurrentLogingUser().getUserName());
		if(insDateSetDto.getQueryOne()==null){
			insDateSetDto.setStatus("Instrument Code is null");
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		int insId = 0;
		try{
			insId = Integer.parseInt(insDateSetDto.getQueryOne());
		}catch(NumberFormatException nfe){
			insDateSetDto.setStatus("Cannot Parse Instrument ID :"+insDateSetDto.getQueryOne());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		InstrumentDao insDao = new InstrumentDao();
		Instrument instrument = insDao.getById(insId);
		if(instrument==null){
			insDateSetDto.setStatus("Cannot Find the Instrument!");
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(instrument.getInstrumentType().getCouponAvailability()!=2){
			insDateSetDto.setStatus("Coupon Dates not applicable for Instrument Type :"+instrument.getInstrumentType().getInstrumentName());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(instrument.getInstrumentCouponDates()!=null && !instrument.getInstrumentCouponDates().isEmpty()){
			String cpnDts = "";
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			for(InstrumentCouponDate icd : instrument.getInstrumentCouponDates()){
				cpnDts = cpnDts+df.format(icd.getCouponDate())+";";
			}
			insDateSetDto.setCouponDates(cpnDts);
		}
		return Action.SUCCESS;
	}
	
	public String getCouponDatesByInsCode(){
		System.out.println("Coupon Date Req from: "+LoginAction.getCurrentLogingUser().getUserName());
		if(insDateSetDto.getQueryOne()==null){
			insDateSetDto.setStatus("Instrument Code is null");
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}

		InstrumentDao insDao = new InstrumentDao();
		Instrument instrument = insDao.getByCode(insDateSetDto.getQueryOne());
		if(instrument==null){
			insDateSetDto.setStatus("Cannot Find the Instrument!");
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(instrument.getInstrumentType().getCouponAvailability()!=2){
			insDateSetDto.setStatus("Coupon Dates not applicable for Instrument Type :"+instrument.getInstrumentType().getInstrumentName());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(instrument.getInstrumentCouponDates()!=null && !instrument.getInstrumentCouponDates().isEmpty()){
			String cpnDts = "";
			DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			for(InstrumentCouponDate icd : instrument.getInstrumentCouponDates()){
				cpnDts = cpnDts+df.format(icd.getCouponDate())+";";
			}
			insDateSetDto.setCouponDates(cpnDts);
		}
		return Action.SUCCESS;
	}
	
	
	public String getFloatingRates(){
		System.out.println("Floating Rate Req from: "+LoginAction.getCurrentLogingUser().getUserName());
		if(insDateSetDto.getQueryOne()==null){
			insDateSetDto.setStatus("Instrument Code is null");
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		int insId = 0;
		try{
			insId = Integer.parseInt(insDateSetDto.getQueryOne());
		}catch(NumberFormatException nfe){
			insDateSetDto.setStatus("Cannot Parse Instrument ID :"+insDateSetDto.getQueryOne());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		InstrumentDao insDao = new InstrumentDao();
		Instrument instrument = insDao.getById(insId);
		if(instrument==null){
			insDateSetDto.setStatus("Cannot Find the Instrument!");
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(instrument.getInstrumentType().getInterestRate()!=InterestRate.V){
			insDateSetDto.setStatus("Floating Rates not applicable for Instrument Type :"+instrument.getInstrumentType().getInstrumentName());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		if(instrument.getInstrumentFloatingRates()!=null && !instrument.getInstrumentFloatingRates().isEmpty()){
			String FlotinRates = "";
			
			for(InstrumentFloatingRate ifr : instrument.getInstrumentFloatingRates()){
				FlotinRates = FlotinRates+df.format(ifr.getStartingDate())+"|"+ifr.getRate()+"|"+df.format(ifr.getNextReset())+";";
			}
			insDateSetDto.setFloatingRates(FlotinRates);
		}
		insDateSetDto.setBenchMark(instrument.getBenchMark());
		insDateSetDto.setResetPeriod(instrument.getResetPeriod());
		insDateSetDto.setFloor(instrument.getFloor().doubleValue());
		insDateSetDto.setCeiling(instrument.getCeiling().doubleValue());
		insDateSetDto.setResetDate(df.format(instrument.getResetDate()));
		return Action.SUCCESS;
	}
	
	public String modifyCouponDates(){
		NewFundDao fundDao = new NewFundDao();
		Fund fund = fundDao.getById(insDateSetDto.getFundId());
		if(fund==null){
			insDateSetDto.setStatus("Cannot Find Fund with ID: "+insDateSetDto.getFundId());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(fund.getUpdateDate()==null){
			insDateSetDto.setStatus("Cannot Find the Last Updated Date for the fund with ID: "+insDateSetDto.getFundId());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		InstrumentDao insDao = new InstrumentDao();
		Instrument instrument = insDao.getById(insDateSetDto.getInstrumentId());
		if(instrument==null){
			insDateSetDto.setStatus("Cannot Find Instrument with ID:" + insDateSetDto.getInstrumentId());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(instrument.getInstrumentType()==null){
			insDateSetDto.setStatus("Cannot Find Instrument Type for Instrument ID:" + insDateSetDto.getInstrumentId());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(instrument.getInstrumentType().getCouponAvailability()!=2){
			insDateSetDto.setStatus("Coupons Not Available for Instrument Type :" + instrument.getInstrumentType().getInstrumentName());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		instrument = new InsDateSetModel().updateCouponDates(instrument, insDateSetDto, fund);
		if(insDao.updateCouponDates(instrument)){
			insDateSetDto.setSuccess(true);
		}else{
			insDateSetDto.setSuccess(false);
			insDateSetDto.setStatus("Database Error while Updating");
		}		
		return Action.SUCCESS;
	}
	
	public String modifyFloatingRates(){
		NewFundDao fundDao = new NewFundDao();
		Fund fund = fundDao.getById(insDateSetDto.getFundId());
		if(fund==null){
			insDateSetDto.setStatus("Cannot Find Fund with ID: "+insDateSetDto.getFundId());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(fund.getUpdateDate()==null){
			insDateSetDto.setStatus("Cannot Find the Last Updated Date for the fund with ID: "+insDateSetDto.getFundId());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		InstrumentDao insDao = new InstrumentDao();
		Instrument instrument = insDao.getById(insDateSetDto.getInstrumentId());
		if(instrument==null){
			insDateSetDto.setStatus("Cannot Find Instrument with ID:" + insDateSetDto.getInstrumentId());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(instrument.getInstrumentType()==null){
			insDateSetDto.setStatus("Cannot Find Instrument Type for Instrument ID:" + insDateSetDto.getInstrumentId());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		if(instrument.getInstrumentType().getInterestRate()!=InterestRate.V){
			insDateSetDto.setStatus("Floating Intersts cannot be applied to Instrument Type :" + instrument.getInstrumentType().getInstrumentName());
			insDateSetDto.setSuccess(false);
			return Action.SUCCESS;
		}
		insDateSetDto.setSuccess(true);
		instrument = new InsDateSetModel().updateFloatingRates(instrument, insDateSetDto, fund);
		if(insDateSetDto.isSuccess()){
			if(insDao.updateFloatingRates(instrument)){
				insDateSetDto.setSuccess(true);
			}else{
				insDateSetDto.setSuccess(false);
				insDateSetDto.setStatus("Database Error while Updating");
			}
		}	
		return Action.SUCCESS;
	}

}
